using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Collections;


namespace MapEditor
{
    class Map
    {

#region Properties

        private Roofs _Roofs;
        public Roofs Roofs
        {
            get { return _Roofs; }
            set { _Roofs = value; }

        }

        private TerrainElements.Terrain[] _TerrainElements;
        public TerrainElements.Terrain[] TerrainElements
        {
            get { return _TerrainElements; }
            set { _TerrainElements=value; }
        }

        //public properties
        private string _Name;
        /// <summary>
        /// Name of the Map
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set {_Name=value;}
        }



        private int _Width;
        /// <summary>
        /// Width of the main playing map in pixels
        /// </summary>
        public int Height
        {
            get { return _Height; }
        }

        private int _Height;
        /// <summary>
        /// Height of the main playing map in pixels
        /// </summary>
        public int Width
        {
            get { return _Width; }
        }


        private Image _BaseImage;
        /// <summary>
        /// Base image of the playing map
        /// </summary>
        public Image BaseImage
        {
            get { return _BaseImage; }
            set 
            { 
                _BaseImage = value;
                _Height = value.Height;
                _Width = value.Width;
                _TerrainArray = Array.CreateInstance(typeof(int), (value.Width / 10), (value.Height / 10));
                _ElevationArray = Array.CreateInstance(typeof(int), (value.Width / 10), (value.Height / 10));
            }
        }

        private Image _OverviewImage;
        /// <summary>
        /// Overview image for the map
        /// </summary>
        public Image OverviewImage
        {
            get { return _OverviewImage; }
            set { _OverviewImage = value; }  
        }

        private Image _ThumbnailImage;
        /// <summary>
        /// Thumbnail image of the playing map
        /// </summary>
        public Image ThumbnailImage
        {
            get { return _ThumbnailImage; }
            set { _ThumbnailImage = value; }  
        }

        private Array _TerrainArray;
        /// <summary>
        /// A 2-dimensional array containing the terrain element codes
        /// </summary>
        public Array TerrainArray
        {
            get { return _TerrainArray; }
            set { _TerrainArray = value; }
        }

        private Array _ElevationArray;

        //A 2-dimensional array containing the Elevation codes
        public Array ElevationArray
        {
            get { return _ElevationArray; }
            set { _ElevationArray = value; }
        }



#endregion




        private readonly int TileSize=120; // TODO get this into a configuration file

        /// <summary>
        /// Returns the name of a terrain element for a given id for this map
        /// </summary>
        /// <param name="ElementID"></param>
        /// <returns></returns>

        public string TerrainElement(int ElementID)
        {
            //TODO remove explicit boxing?
            TerrainElements.Terrain myTerrain;
            myTerrain = (TerrainElements.Terrain)_TerrainElements[ElementID];
            return myTerrain.Name;
        }

        /// <summary>
        /// creates a folder, and deletes the current one if it exists
        /// </summary>
        /// <param name="saveFolder"></param>
        private static void CreateSaveFolder(string saveFolder)
        {

            //first create the save foler, deleting the previous one if it exists
            if (Directory.Exists(saveFolder))
            {
                Directory.Delete(saveFolder, true);
            }
            Directory.CreateDirectory(saveFolder);

        }


        /// <summary>
        /// Map class constructor
        /// </summary>
        /// <param name="MapImage"></param>
        public Map(string inMapName)
        {
            _Name = inMapName;
        }

        public bool LoadTerrain()
        {
        
            return true;

        }

        



        /// <summary>
        /// Saves the base map as a series of tiles
        /// </summary>
        /// <param name="saveLocation"></param>
        /// <param name="tileSize"></param>
        /// <returns></returns>
        public bool CreateMapTiles(string saveLocation)
        {
            try
            {
                string SaveFolder = saveLocation + this.Name + "\\tiles\\";
                CreateSaveFolder(SaveFolder);

                //create a new bitmap
                //and fill it with the first 32 pixels of the mapImage
                System.Drawing.Bitmap tile = new System.Drawing.Bitmap(TileSize, TileSize);

                //first convert the mapImage to a bitmap
                Bitmap mapBitmap = new Bitmap(this.BaseImage);

                int MapX;
                int MapY;

                //get the size of the original bitmap in multiples of the tilesize
                MapX = mapBitmap.Width / TileSize;
                MapY = mapBitmap.Height / TileSize;

                //loop through the map x and map y
                int looperX;
                int looperY;

                for (looperX = 0; looperX < MapX; looperX++)
                {
                    for (looperY = 0; looperY < MapY; looperY++)
                    {
                        //get the bitmap for this section of the main map and write it to a file

                        //create a rectangle at the tile position of the main map
                        Rectangle cloneRect = new Rectangle(looperX * TileSize, looperY * TileSize, TileSize, TileSize);
                        //get the pixel format of the main map
                        System.Drawing.Imaging.PixelFormat format = mapBitmap.PixelFormat;
                        //create a clone bitmap of that section of the main map
                        Bitmap cloneBitmap = mapBitmap.Clone(cloneRect, format);


                        //load the created bitmap into one of the picture boxes
                        //create a new Image using this bitmap and save it

                        string MapLocation = Convert.ToString(looperX).PadLeft(4, Convert.ToChar("0")) + Convert.ToString(looperY).PadLeft(4, Convert.ToChar("0"));
                        string TileSavePath = SaveFolder + "\\TILE" + MapLocation + ".jpg";
                        cloneBitmap.Save(TileSavePath);
                    }
                }
                return true;
            }
                        
            catch (Exception exception)
            {
                System.Windows.Forms.MessageBox.Show(exception.Message);
                return false;
            }
        }



    }
}
